Why Use C#, VB.NET to Merge Word Documents?
Most people commonly meet such a case that they must merge several documents into one document. For those seldom need to combine less than ten documents into one, they use the method to paste the first file into the second, and paste the second to the third, then…until the tenth file. However, for those frequently consolidate more than ten documents to one file, obviously, this solution is not feasible. This measure can bring a lot of troubles, such as some information can be easily lost or repeated, much time would be spent and so on. Fortunately, using C#, VB.NET to merge word documents is much easier than the paste the file one by one. You only need to compose the right codes, the documents can be merged together in a rapid speed. So using C#, VB.NET is the best choice.
How to Use C#, VB.NET to Merge Word Documents?
Spire.Doc, as a professional and powerful Word component, enables its users to perform a wide range of processing tasks directly without Microsoft Office Word installed in the computer. It also allows merging word documents together.
Before we start, we need to install Spire.Doc and Visual Studio in the computer. If you have no Spire.Doc, you can freely download it here.
Most people commonly meet such a case that they must merge several documents into one document. For those seldom need to combine less than ten documents into one, they use the method to paste the first file into the second, and paste the second to the third, then…until the tenth file. However, for those frequently consolidate more than ten documents to one file, obviously, this solution is not feasible. This measure can bring a lot of troubles, such as some information can be easily lost or repeated, much time would be spent and so on. Fortunately, using C#, VB.NET to merge word documents is much easier than the paste the file one by one. You only need to compose the right codes, the documents can be merged together in a rapid speed. So using C#, VB.NET is the best choice.
How to Use C#, VB.NET to Merge Word Documents?
Spire.Doc, as a professional and powerful Word component, enables its users to perform a wide range of processing tasks directly without Microsoft Office Word installed in the computer. It also allows merging word documents together.
Before we start, we need to install Spire.Doc and Visual Studio in the computer. If you have no Spire.Doc, you can freely download it here.
Step 1
Create the Word Merge project in Visual Studio. Then, add a button in Form1 and openFileDialog. Finally, click the button and add Spire.Doc dill as reference.
Step 2
Add the following code to the top of the file..
C#
using Spire.Doc;
using Spire.CompoundFile;
using Spire.Doc.Documents;
VB.NET
Imports Spire.Doc;
Imports Spire.Doc.Documents;
Imports Spire.CompoundFile;
Step 3
Add the following codes to the method.
C#
private void button1_Click(object sender, EventArgs e)
{
string fileName = OpenFile();
string fileMerge = OpenFile();
if ((!string.IsNullOrEmpty(fileName)) && (!string.IsNullOrEmpty(fileMerge)))
{
//Create word document
Document document = new Document();
document.LoadFromFile(fileName,FileFormat.Doc);
Document documentMerge = new Document();
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
foreach( Section sec in documentMerge.Sections)
{
document.Sections.Add(sec.Clone());
}
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
}
private string OpenFile()
{
openFileDialog1.Filter = "Word Document (*.doc)|*.doc";
openFileDialog1.Title = "Choose a document to merage";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
return openFileDialog1.FileName;
}
return string.Empty;
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Add the following codes to the method.
C#
private void button1_Click(object sender, EventArgs e)
{
string fileName = OpenFile();
string fileMerge = OpenFile();
if ((!string.IsNullOrEmpty(fileName)) && (!string.IsNullOrEmpty(fileMerge)))
{
//Create word document
Document document = new Document();
document.LoadFromFile(fileName,FileFormat.Doc);
Document documentMerge = new Document();
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
foreach( Section sec in documentMerge.Sections)
{
document.Sections.Add(sec.Clone());
}
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
}
private string OpenFile()
{
openFileDialog1.Filter = "Word Document (*.doc)|*.doc";
openFileDialog1.Title = "Choose a document to merage";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
return openFileDialog1.FileName;
}
return string.Empty;
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
VB.NET
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim fileName As String = OpenFile()
Dim fileMerge As String = OpenFile()
If ((Not String.IsNullOrEmpty(fileName))) AndAlso ((Not String.IsNullOrEmpty(fileMerge))) Then
'Create word document
Dim document_Renamed As New Document()
document_Renamed.LoadFromFile(fileName,FileFormat.Doc)
Dim documentMerge As New Document()
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc)
For Each sec As Section In documentMerge.Sections
document_Renamed.Sections.Add(sec.Clone())
Next sec
'Save doc file.
document_Renamed.SaveToFile("Sample.doc", FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End If
End Sub
Private Function OpenFile() As String
openFileDialog1.Filter = "Word Document (*.doc)|*.doc"
openFileDialog1.Title = "Choose a document to merage"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
Return openFileDialog1.FileName
End If
Return String.Empty
End Function
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
Step 4
Press F5 to run the project, Form1 appears, you can click the button in Form1, a dialog box is opened for you to choose documents that you want to merge. Then you can see the document in the Word Merge project in which you merge all the documents information together.
Please look at the screenshot:
Press F5 to run the project, Form1 appears, you can click the button in Form1, a dialog box is opened for you to choose documents that you want to merge. Then you can see the document in the Word Merge project in which you merge all the documents information together.
Please look at the screenshot:
If you are interested in the above codes, you can click here to consualt more. Or If you confront some problems during you run the project according to the above method, you can leave your question on the forum, there are professionals ready to help you at any time.
Word Forum